package com.picshare.service;

import com.picshare.model.ApiKey;
import com.picshare.model.FileItem;
import com.picshare.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Service
public class ClientService {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ApiService apiService;
    
    @Autowired
    private FileService fileService;
    
    public ResponseEntity<Map<String, Object>> login(String username, String password) {
        User user = userService.login(username, password);
        
        if (user != null) {
            Map<String, Object> successResponse = new HashMap<>();
            successResponse.put("success", true);
            successResponse.put("message", "登录成功");
            successResponse.put("username", user.getUsername());
            successResponse.put("nickname", user.getNickname());
            return ResponseEntity.ok(successResponse);
        } else {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "用户名或密码错误");
            return ResponseEntity.status(401).body(errorResponse);
        }
    }
    
    public ResponseEntity<Map<String, Object>> uploadFile(MultipartFile file, String apiKey) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 验证API Key
            ApiKey keyInfo = apiService.getApiKeyInfo(apiKey);
            if (keyInfo == null) {
                result.put("success", false);
                result.put("message", "API Key无效");
                return ResponseEntity.status(401).body(result);
            }
            
            if (file.isEmpty()) {
                result.put("success", false);
                result.put("message", "文件不能为空");
                return ResponseEntity.badRequest().body(result);
            }
            
            // 使用现有的FileService上传文件
            FileItem fileItem = fileService.uploadFile(file, keyInfo.getUsername());
            
            if (fileItem == null) {
                result.put("success", false);
                result.put("duplicate", true);
                result.put("message", "文件已存在");
            } else {
                result.put("success", true);
                result.put("id", fileItem.getId());
                result.put("fileName", fileItem.getFileName());
                result.put("originalName", fileItem.getOriginalName());
                result.put("fileSize", fileItem.getSize());
                result.put("fileType", fileItem.getType());
                result.put("isImage", fileItem.isImage());
                result.put("uploadTime", fileItem.getUploadTime());
                result.put("message", "文件上传成功");
            }
            
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "文件上传失败: " + e.getMessage());
            return ResponseEntity.status(500).body(result);
        }
    }
}